<template>
  <div>
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-form-item label="岗位名称">
        <el-input v-model="query.postName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :loading="queryLoading" @click="getPage()">查询</el-button>
        <el-button @click="reset">重置</el-button>
        <el-button v-if="$hasApiPerm('post:add')" type="success" @click="showAdd()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      style="width: 100%;margin-bottom: 20px;"
      border
      :header-cell-style="{textAlign : 'center'}"
      :cell-style="{textAlign : 'center'}"
    >
      <el-table-column
        type="index"
        label="序号"
        width="100"
      >
      </el-table-column>
      <el-table-column
        prop="postName"
        label="岗位名称"
      >
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
      >
      </el-table-column>
      <el-table-column
        prop="remark"
        label="岗位备注"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="160"
      >
        <template slot-scope="{row}">
          {{ resolveTime(row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button v-if="$hasApiPerm('post:get')" type="text" size="small" @click="showView(row.id)">查看</el-button>
          <el-button v-if="$hasApiPerm('post:update')" type="text" size="small" @click="showUpdate(row.id)">编辑</el-button>
          <el-button v-if="$hasApiPerm('post:del')" type="text" size="small" @click="ensureDelete(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="total,prev, pager, next"
      :current-page.sync="page"
      :page-size="pageSize"
      :total="total"
      @current-change="getPage"
    >
    </el-pagination>
    <opt-post :dialog-visible.sync="optDialog" :opt-flag="optFlag" :post-id="postId"
              @refresh="getPage"
    />
  </div>
</template>

<script>
import postApi from '@/api/system/postApi'
import OptPost from '@/views/system/post/components/OptPost.vue'
import moment from 'moment'

export default {
  name: 'PostList',
  components: { OptPost },
  data() {
    return {
      query: {},
      page: 1,
      pageSize: 10,
      queryLoading: false,
      dataList: [],
      total: 0,
      optDialog: false,
      optFlag: '',
      postId: null
    }
  },
  created() {
    this.getPage()
  },
  methods: {
    getPage(pageNum = 1) {
      this.queryLoading = true
      this.page = pageNum
      postApi.getPage(this.page, this.pageSize, this.query).then(res => {
        this.dataList = res.data.list
        this.total = res.data.total
      }).finally(() => {
        this.queryLoading = false
      })
    },
    resolveTime(time) {
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    showView(postId) {
      this.optDialog = true
      this.optFlag = 'view'
      this.postId = postId
    },
    showAdd() {
      this.optDialog = true
      this.optFlag = 'add'
      this.postId = null
    },
    showUpdate(postId) {
      this.optDialog = true
      this.optFlag = 'update'
      this.postId = postId
    },
    ensureDelete(postId) {
      this.$confirm('确定执行删除操作吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteOpt(postId)
      }).catch(() => {
      })
    },
    deleteOpt(postId) {
      postApi.delete(postId).then(res => {
        this.$message({
          message: '操作成功！',
          type: 'success'
        })
        this.getPage()
      })
    },
    reset() {
      this.query = {}
    }
  }
}
</script>

<style scoped>
.el-pagination {
  text-align: right;
}
</style>
